package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseAction;
import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.EntityRelate;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.EntityRelateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller("/powerdb/designer/entityRelate")
@RequestMapping("/powerdb/designer/entityRelate")
public class EntityRelateAction extends BaseNewAction<EntityRelate, Long> {

	@Override
	@Resource(name = "/powerdb/designer/entityRelateService")
	public void setService(BaseService<EntityRelate, Long> service) {
		super.service = service;
	}

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult find(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.find(filter);
	}


	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/pages", method = RequestMethod.PATCH)
	@ResponseBody
	public PageResult findPages(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.findPages(filter);
	}


	/**
	 * 查询
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "对象查询")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult get(@PathVariable("id") long id, HttpServletRequest request) {
		return super.get(id);
	}


	/**
	 * 新增
	 *
	 * @param obj
	 * @return JsonResult
	 */
	@MethodName(name = "新增")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add(EntityRelate obj) {
		return super.add(obj);
	}


	/**
	 * 更新
	 *
	 * @param obj
	 * @return JsonResult
	 */
	@MethodName(name = "修改")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResult update(EntityRelate obj) {
		return super.update(obj);
	}


	/**
	 * 删除
	 *
	 * @param id
	 * @return JsonResult
	 */
	@MethodName(name = "删除")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResult delete(@PathVariable("id") long id) {
		return super.delete(id);
	}

	/**
	 * 批量删除
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return JsonResult
	 */
	@MethodName(name = "批量删除")
	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult batchDelete(@RequestParam(value = "ids", required = true) String ids) {
		return super.delete(ids);
	}


	@MethodName(name = "更新坐标字符串")
	@RequestMapping(value = "/points", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updatePoints(@RequestParam(value = "id", required = true) Long id,
	                               @RequestParam(value = "points", required = true) String points,
	                               @RequestParam(value = "multiplicityJson", required = true) String multiplicityJson) {
		JsonResult json = new JsonResult();
		try {
			int re = ((EntityRelateService) service).updatePoints(id, points, multiplicityJson);
			json.setObj(re);
		} catch (Exception e) {
			setErrMsg("", LOG, json, e);
		}

		return json;
	}

	/**
	 * 根据关联实体id获取关联实体
	 *
	 * @param id
	 * @return JsonResult
	 */
	@MethodName(name = "获取连线的两个实体")
	@RequestMapping(value = "/getEntityByEntityRelatId", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getEntityByEntityRelatId(@RequestParam(value = "id", required = true) Long id) {
		JsonResult json = new JsonResult(true);

		try {
			json.setObj(((EntityRelateService) service).getEntitysByEntityRelatId(id));
			json.setMsg("查询成功");
		} catch (Exception e) {
			BaseAction.setErrMsg("查询失败", LOG, json, e);
		}
		return json;

	}

}
